package com.eng.scc.core.parts;

import com.eng.scc.core.data.Company;
import com.eng.scc.core.util.ImageFinder;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JLabel;
import javax.swing.JList;

/**
 *
 * @author Rafael
 */
public class CompaniesList extends JList
{

    private List<Company> companies = new ArrayList<Company>();

    public CompaniesList()
    {
//        setMRowHeight( 25 );
        setCellRenderer( new CompanyRenderer() );
    }

    public void setCompanies( List<Company> data )
    {
        this.companies = data;

        setListData( this.companies.toArray() );
    }

    public Company getSelectedCompany()
    {
        if ( getSelectedIndex() >= 0 )
        {
            return companies.get( getSelectedIndex() );
        }

        return null;
    }

    class CompanyRenderer extends DefaultListCellRenderer
    {

        public Component getListCellRendererComponent( JList list, Object value,
                                                       int index, boolean isSelected, boolean cellHasFocus )
        {
            JLabel lbl = ( JLabel ) super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus );
            lbl.setPreferredSize( new Dimension( lbl.getPreferredSize().width, 25 ) );
            lbl.setFont( new Font( Font.SANS_SERIF, Font.PLAIN, 12 ) );
            lbl.setIcon( ImageFinder.getImageIcon( "tree_company.png" ) );
            
            return lbl;
        }
    }
}
